package com.kyh.util.sms.aliyun;

import java.math.BigDecimal;
import java.util.HashMap;

import org.omg.CORBA.SystemException;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.kyh.util.JsonUtil;

/**
 * 阿里云官方发送短信
 * @author user
 *
 */
public class ALiYunOffSms extends SMS {
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
//    static final String accessKeyId = "LTAIVROfu1TzglNt";
//    static final String accessKeySecret = "2AIOAdkf6f84PJkMvngxT2UPVffvlK";
	static final String accessKeyId = "LTAI4GCWyKoywXBbgACkkfGz";
	static final String accessKeySecret = "znw6mwxjmzS8C9S9egjsnWSlrNtNgL";
	
	/**
	 * 发送短信
	 * @param phone  手机号码
	 * @param para   参数
	 * @param tempCode  模版量
	 * @throws ClientException 
	 */
	public static void sendMsg(String phone, String para, String tempCode) throws ClientException
	{

		//可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("小e生活");
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(tempCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam(para);

        //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        //request.setSmsUpExtendCode("90997");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse response = acsClient.getAcsResponse(request);
        
        System.out.println("短信接口返回的数据----------------");
//        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
//        System.out.println("RequestId=" + response.getRequestId());
//        System.out.println("BizId=" + response.getBizId());
	}
	
	/**
	 * 取消记录
	 * @param amount
	 * @param phone
	 */
	public static void cancelPlan(BigDecimal amount, String phone) {
		JSONObject map = new JSONObject();
		map.put("name", amount);

		try {
			sendMsg(phone, map.toString(), "SMS_135800519");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 计划成功
	 * @param phone
	 */
	public static void planSuccess(String phone) {
		try {
			sendMsg(phone, "", "SMS_135765511");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 计划失败
	 * @param phone
	 * @throws SystemException 
	 */
	public static void planFail(String phone, String reason) throws SystemException {
		JSONObject map = new JSONObject();
		map.put("reason", reason);
		try {
			sendMsg(phone, map.toString(), "SMS_136381842");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提现成功
	 * @param phone
	 */
	public static void withdrawSuccess(String phone, BigDecimal amount) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("amount", amount);
		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_146290437");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提现失败
	 * @param phone
	 */
	public static void withdrawFail(String phone, String reason) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("reason", reason);

		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_136391405");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 升级成功
	 * @param phone
	 */
	public static void upgradeSuccess(String phone, String name) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);

		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_136392103");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 短信验证码
	 * @param phone
	 */
	public static void sendVerifuCode(String phone, String code) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);

		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_136866138");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 还款日提醒
	 * @param phone
	 * @throws SystemException 
	 */
	public static void cardRepay(String phone, String name) throws SystemException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);

		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_140735258");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 提交进件申请
	 * @param phone
	 * @throws SystemException
	 */
	public static void settleAuditApy(String phone) throws SystemException {
		try {
			sendMsg(phone, "", "SMS_151991833");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 进件审核通过
	 * @param phone
	 * @throws SystemException
	 */
	public static void settleAuditSuccess(String phone) throws SystemException {
		try {
			sendMsg(phone, "", "SMS_151996843");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 进件审核失败
	 * @param phone
	 * @param name
	 * @throws SystemException
	 */
	public static void settleAuditFail(String phone, String name) throws SystemException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);

		try {
			sendMsg(phone, JsonUtil.mapToJson(map), "SMS_151991845");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 计划卡住短信通知
	 * @param phone
	 * @throws SystemException
	 */
	public static void replanUnExecute(String phone,String planInfo) throws SystemException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", planInfo);
		try {
			sendMsg(phone,  JsonUtil.mapToJson(map), "SMS_174989071");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 短信验证码（商户入驻成功）
	 * @param phone
	 */
	public static void storeInSuccess(String phoneReceive, String phone, String code) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("phone", phone);
		map.put("code", code);

		try {
			sendMsg(phoneReceive, JsonUtil.mapToJson(map), "SMS_190720121");
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}
}
